#include <gui/gui.h>

// GUI STATE /////////////////////////////////////////////

void GuiState::AddControl(GuiControl * control)  {
   _controls.push_back(control);
   _root->addChild(control);
}

GuiState::~GuiState()  {
   std::list<GuiControl *>::iterator i;
   for (i = _controls.begin(); i != _controls.end(); i++)
      delete *i;
}

void GuiState::Draw(Device * device, int mouseX, int mouseY, double tElapsed)  {
   std::list<GuiControl *>::iterator i;
   for (i = _controls.begin(); i != _controls.end(); i++)  {
      if ((*i)->IsVisible())  {
         (*i)->Draw(device, tElapsed);
      }
   }
}

void GuiState::Update(int mouseX, int mouseY, unsigned long key, double tElapsed)  {
   std::list<GuiControl *>::iterator i;
   for (i = _controls.begin(); i != _controls.end(); i++)  {
      if ((*i)->IsEnabled())  {
         (*i)->Update(mouseX, mouseY, key, tElapsed);
      }
   }
}

// GUI CONTROLLER ////////////////////////////////////////

GuiController::GuiController(Device * device) : 
   _consoleInput(0xFFFFFFFF, 0xAA333333, device->deviceWidth() / -2.0f, device->deviceHeight() / -2.0f, device->deviceWidth(), 18.0f) 
{
   _rootNode = new SceneNode();
   _focusState = 0;

   // console init
   _show_console = false;
   _consoleInput.SetText(std::string("Test"));
   device->createEmptyTexture(&_console_bg, 1, 1);
   _console_font = device->createFont("tahoma", 12);
}

GuiController::~GuiController()  {
   delete _rootNode;

   // console
   _console_bg->Release();
}

void GuiController::DrawGui(Device * device, double tElapsed)  {
   // input
   long mX = 0, mY = 0;

   // filter states and render appropriate ones
   std::vector<GuiState *>::iterator i;
   for (i = _states.begin(); i != _states.end(); i++)  {
      if ((*i)->IsVisible())
         (*i)->Draw(device, mX, mY, tElapsed);
   }

   //if (_states.size() > 0)
      //;//_states.at(_focusState)->Update(mX, mY, 0);
}

void GuiController::AddState(GuiState * state)  {
   _states.push_back(state);
}

void GuiController::SetFocusState(GuiState * state)  {
   for (unsigned int i = 0; i < _states.size(); i++)  {
      if (_states.at(i) == state)  { 
         _focusState = i;
         return;
      }
   }
}

void GuiController::UpdateConsole(long key, double tElapsed) {
   _consoleInput.Update(0, 0, key, tElapsed);
}

void GuiController::DrawConsole(Device * device, std::list<std::string>& lines, std::list<int> lheight) const {
  if(_show_console) {

    device->setColor(0x7fFFFFFF);
    device->setTexture(_console_bg);
    device->drawRect(0, 0, device->deviceWidth(), device->deviceHeight());

    std::list<std::string>::iterator istring = lines.begin();
    std::list<int>::iterator iint = lheight.begin();

    for(int text_room = device->deviceHeight(); istring != lines.end() && text_room > 0; istring++, iint++) {
      if(*iint == 0)
	  	   *iint = device->drawText((*istring).c_str(), _console_font, 0, 0, 800, text_room, 0x00ffffff, DT_BOTTOM & DT_LEFT & DT_WORDBREAK);

		text_room -= device->drawText((*istring).c_str(), _console_font, 0, text_room - *iint - 20 /* space for input */, 600, *iint, 0xffffffff, DT_BOTTOM & DT_LEFT & DT_WORDBREAK);
	 }

    _consoleInput.Draw(device, 0);

    device->setColor(0xffffffff);
  }
}
